﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using System.Web;

namespace WebApiVersionControl.Helper
{
    #region sqlhelper
    public class DbHelper
    {
        public static string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;

        public static SqlConnection conn = null;

        public static SqlDataReader MyDataReaderSql(string sql)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static int MyExNonQuerySql(string sql)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand command = new SqlCommand(sql, conn);
                return command.ExecuteNonQuery();
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static object MyExScanlarSql(string sql)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteScalar();
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static SqlDataReader MyDataReaderProc(string proc, SqlParameter[] parameters)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(proc, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                if (parameters != null) cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static int MyExNonQueryProc(string proc, SqlParameter[] sqlParameters)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(proc, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                if (sqlParameters != null) cmd.Parameters.AddRange(sqlParameters);
                return cmd.ExecuteNonQuery();
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static int MyExNonQueryArray(string proc, string code, string[] str)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(proc, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter(code, SqlDbType.VarChar, 50));
                int num = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    cmd.Parameters[code].Value = str[i];
                    int count = cmd.ExecuteNonQuery();
                    if (count > 0) num++;
                }
                return num;
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static object MyExScanlarProc(string proc, SqlParameter[] parameters)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand cmd = new SqlCommand(proc, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                if (parameters != null) cmd.Parameters.AddRange(parameters);
                object obj = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return obj;
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static DataSet MyDataSetSql(string sql)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                return ds;
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }

        public static DataTable MyDataTableProc(string proc, SqlParameter[] parameters)
        {
            return MyDataSetProc(proc, parameters).Tables[0];
        }

        private static DataSet MyDataSetProc(string proc, SqlParameter[] parameters)
        {
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                SqlDataAdapter adp = new SqlDataAdapter(proc, conn);
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (parameters != null)
                {
                    adp.SelectCommand.Parameters.AddRange(parameters);
                }
                DataSet ds = new DataSet();
                adp.Fill(ds);
                return ds;
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
                throw;
            }
        }


        /// <summary>
        /// 32位 MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encode32(string password)
        {
            MD5 mD5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] targetData = mD5.ComputeHash(fromData);
            string byte2String = null;
            for (int i = 0; i < targetData.Length; i++)
            {
                //这个是很常见的错误，你字节转换成字符串的时候要保证是2位宽度啊，
                //某个字节为0转换成字符串的时候必须是00的，否则就会丢失位数啊。不仅是0，1～9也一样。
                //byte2String += targetData[i].ToString("x");//这个会丢失
                byte2String = byte2String + targetData[i].ToString("x2");
            }
            Random random = new Random();
            string firstEncode = random.Next(100000, 999999).ToString();
            string lastEncode = random.Next(1000, 9999).ToString();
            return firstEncode + byte2String + lastEncode;
        }
    }
    #endregion
}